package com.example.demo.web;

import com.example.demo.dao.CustomerRepository;
import com.example.demo.entity.Customer;
import com.example.demo.service.MainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @auther yaoyouwei
 * @create 2020-03-22 22:59
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerRepository repository;

    @Autowired
    MainService mainService;

    /**
     * 初始化数据
     */
    @PostMapping("/batchAdd")
    public String index() {
        // save a couple of customers
//        repository.save(new Customer("Jack", "Bauer"));
//        repository.save(new Customer("Chloe", "O'Brian"));
//        repository.save(new Customer("Kim", "Bauer"));
//        repository.save(new Customer("David", "Palmer"));
//        repository.save(new Customer("Michelle", "Dessler"));
//        repository.save(new Customer("Bauer", "Dessler"));
        return "success";
    }

    /**
     * 查询所有
     */
    @GetMapping("/findAll")
    public List<Customer> findAll(){
        List<Customer> result = repository.findAll();
        for (Customer customer:result){
            System.out.println(customer.toString());
        }

        System.out.println("-------------------------------------------");
        return result;
    }


    /**
     * 事务测试
     */
    @GetMapping("/execute")
    public String execute(){
        mainService.executeJpa();
        return "success";
    }


}